<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Common\Server;
use App\Common\Demand\Server\Utils as UtilsInterfaces;
class Utils implements UtilsInterfaces
{
    // error function
    public function lxErrorReturn(string $msg,int $code,array $data=[]):array{
        $responseData['meta'] = ['code' => $code, 'msg' => $msg];
        if (!empty($data)) {
            $responseData['data'] = $data;
        }
        return $responseData;
    }
    //success function
    public function lxSuccessReturn(string  $msg,array $data=[]):array{
        $responseData['meta'] = ['code' => 0, 'msg' => $msg];
        if (!empty($data)) {
            $responseData['data'] = $data;
        }

        return $responseData;
    }
    public function getSortData($field,$sortType,$key): array{
        if (isset($sortType)&& !empty($field)) {
            $sortData['field'] = $field;
        } else {
            $sortData['field'] = $key;
        }
        if (!empty($sortType)) {
            if ($sortType == 'descending') {
                $sortData['type'] = 'desc';
            } else {
                $sortData['type'] = 'asc';
            }	
        } else {
            $sortData['type'] = 'desc';
        }
        return $sortData;
    }
    public function getPageLimitData($page,$limit): array{
        if (isset($limit) && $limit > 5) {
            $pager['limit'] = $limit;
        } else {
            $pager['limit'] = 6;
        }
        if (isset($page) && $page > 0) {
            $pager['page'] = $page;
        } else {
            $pager['page'] = 1;
        }
        $pager['start'] = ($pager['page'] - 1) * $pager['limit'];
        return $pager;
    }
}